package com.extension.company.common.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class StringUtils {

	private final static Pattern emailer = Pattern
			.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	private final static SimpleDateFormat dateFormater = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
	private final static SimpleDateFormat dateFormater2 = new SimpleDateFormat(
			"yyyy-MM-dd", Locale.getDefault());
	private final static SimpleDateFormat dateFormater3 = new SimpleDateFormat(
			"HH:mm", Locale.getDefault());
	private final static SimpleDateFormat dateFormater4 = new SimpleDateFormat(
			"MM-dd HH:mm", Locale.getDefault());
	private final static SimpleDateFormat year = new SimpleDateFormat("yyyy",
			Locale.getDefault());
	private final static SimpleDateFormat day = new SimpleDateFormat("dd",
			Locale.getDefault());
	private final static SimpleDateFormat month = new SimpleDateFormat("MM",
			Locale.getDefault());

	/**
	 * 日期格式
	 */
	public static final String PATTERN1 = "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN2 = "yyyy/MM/dd HH:mm:ss";
	public static final String PATTERN3 = "yyyy-MM-dd";
	public static final String PATTERN4 = "yyyy/MM/dd";
	public static final String PATTERN5 = "yyyy年MM月dd日";
	public static final String PATTERN6 = "yyyy-MM-dd'T'HH:mm:ss";
	public static final String PATTERN7 = "HH:mm:ss";
	public static final String PATTERN8 = "HH:mm";
	public static final String PATTERN9 = "yyyy-MM-dd HH:mm";
	public static final String PATTERN10 = "HH";
	public static final String PATTERN11 = "H";
	public static final String PATTERN12 = "MM-dd HH:mm";
	public static final String PATTERN13 = "yyyy";
	public static final String PATTERN14 = "MM";
	public static final String PATTERN15 = "dd";

	/**
	 * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
	 *
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断给定List是否为空。 若输入字符串为null或空字符串，返回true
	 *
	 * @param list
	 * @return boolean
	 */
	public static boolean isEmpty(List<?> list) {
		if (list == null) {
			return true;
		}
		if (list.size() <= 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断给定EditText是否为空。 若输入字符串为null或空字符串，返回true
	 *
	 * @param et
	 * @return
	 */
	public static boolean isEmpty(EditText et) {
		if (et.getText().toString().trim().equals(""))
			return true;

		return false;
	}

	/**
	 * 判断给定EditText是否为空。 若输入字符串为null或空字符串，返回true
	 *
	 * @param et
	 * @return
	 */
	public static boolean isEmpty(TextView et) {
		if (et.getText().toString().trim().equals(""))
			return true;

		return false;
	}

	/**
	 * 获取EditText的文字
	 *
	 * @param et
	 * @return
	 */
	public static String getEditText(EditText et) {
		return et.getText().toString().trim();
	}

	/**
	 * 获取TextView的文字
	 *
	 * @param et
	 * @return
	 */
	public static String getText(TextView et) {
		return et.getText().toString().trim();
	}

	/**
	 * 获取button
	 */
	public static String getButtonText(Button btn) {
		return btn.getText().toString().trim();
	}

	/**
	 * 判断是否是密码   6-14位数字、字母和_
	 *
	 * @param str
	 * @return
	 */
	public static boolean isPwd(String str) {
		String regEx = "^\\w{6,14}$";
		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(str);
		boolean rs = mat.find();
		return rs;
	}

	/**
	 * 判断是否为一个合法的日期
	 *
	 * @param str 输入的日期
	 * @return convertSuccess 如果合法返回true否则返回false
	 */
	@SuppressLint("SimpleDateFormat")
	public static boolean isValidDate(String str) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		try {
			format.setLenient(false);
			format.parse(str);//

		} catch (ParseException e) {
			e.printStackTrace();
			convertSuccess = false; // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
		}
		return convertSuccess;
	}


	/**
	 * 对象转整数
	 *
	 * @param obj
	 * @return 转换异常返回 0
	 */
	public static long toLong(String obj) {
		try {
			return Long.parseLong(obj);
		} catch (Exception e) {
		}
		return 0;
	}

	/**
	 * 对象转整数
	 *
	 * @param obj
	 * @return 转换异常返回 0
	 */
	public static int toInt(String obj) {
		try {
			return Integer.parseInt(obj);
		} catch (Exception e) {
		}
		return 0;
	}

	/*
	 * public static String getImsi(Context context) { String imsi = null; try {
	 * TelephonyManager mTelephonyMgr = (TelephonyManager)
	 * context.getSystemService(Context.TELEPHONY_SERVICE); imsi =
	 * mTelephonyMgr.getSubscriberId();
	 *
	 * //取imsi为空时 用生成的UUID来唯一标示手机 if (null == imsi || "".equals(imsi)) { imsi =
	 * new MyApplication().getAppId(); } } catch (Exception e) { imsi = new
	 * MyApplication().getAppId(); } return imsi; }
	 */

	/**
	 * 将%E4%BD%A0类型转换为汉字
	 *
	 * @param s url编码类型
	 * @return
	 */
	public static String unescape(String s) {
		StringBuffer sbuf = new StringBuffer();
		int l = s.length();
		int ch = -1;
		int b, sumb = 0;
		for (int i = 0, more = -1; i < l; i++) {
			/* Get next byte b from URL segment s */
			switch (ch = s.charAt(i)) {
				case '%':
					ch = s.charAt(++i);
					int hb = (Character.isDigit((char) ch) ? ch - '0'
							: 10 + Character.toLowerCase((char) ch) - 'a') & 0xF;
					ch = s.charAt(++i);
					int lb = (Character.isDigit((char) ch) ? ch - '0'
							: 10 + Character.toLowerCase((char) ch) - 'a') & 0xF;
					b = (hb << 4) | lb;
					break;
				case '+':
					b = ' ';
					break;
				default:
					b = ch;
			}
			/* Decode byte b as UTF-8, sumb collects incomplete chars */
			if ((b & 0xc0) == 0x80) { // 10xxxxxx (continuation byte)
				sumb = (sumb << 6) | (b & 0x3f); // Add 6 bits to sumb
				if (--more == 0)
					sbuf.append((char) sumb); // Add char to sbuf
			} else if ((b & 0x80) == 0x00) { // 0xxxxxxx (yields 7 bits)
				sbuf.append((char) b); // Store in sbuf
			} else if ((b & 0xe0) == 0xc0) { // 110xxxxx (yields 5 bits)
				sumb = b & 0x1f;
				more = 1; // Expect 1 more byte
			} else if ((b & 0xf0) == 0xe0) { // 1110xxxx (yields 4 bits)
				sumb = b & 0x0f;
				more = 2; // Expect 2 more bytes
			} else if ((b & 0xf8) == 0xf0) { // 11110xxx (yields 3 bits)
				sumb = b & 0x07;
				more = 3; // Expect 3 more bytes
			} else if ((b & 0xfc) == 0xf8) { // 111110xx (yields 2 bits)
				sumb = b & 0x03;
				more = 4; // Expect 4 more bytes
			} else /* if ((b & 0xfe) == 0xfc) */ { // 1111110x (yields 1 bit)
				sumb = b & 0x01;
				more = 5; // Expect 5 more bytes
			}
			/* We don't test if the UTF-8 encoding is well-formed */
		}
		return sbuf.toString();
	}

	/**
	 * 将字符串转位日期类型
	 *
	 * @param sdate
	 * @return
	 */
	public static Date toDate(String sdate) {
		if (isEmpty(sdate))
			return null;
		try {
			return dateFormater.parse(sdate);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 将long类型转换为日期
	 *
	 * @param sdate
	 * @return
	 */
	public static String toDate(long sdate, String pattern) {
		String time = "";
		try {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			Date date = new Date(sdate);
			time = format.format(date);
			return time;
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取多少天之前的日期
	 *
	 * @param day     天数
	 * @param pattern 日期格式
	 * @return
	 */
	public static String getDateBefore(int day, String pattern) {
		String time = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, -day);//往上推一天  30 推三十天  365推一年
			Date mBefore = calendar.getTime();
			time = sdf.format(mBefore);
		} catch (Exception e) {
			time = "";
		}
		return time;
	}

	/**
	 * 获取多少天之后的日期
	 *
	 * @param day     天数
	 * @param pattern 日期格式
	 * @return
	 */
	public static String getDateAfter(int day, String pattern) {
		String time = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, day);//往后推一天  30 推三十天  365推一年
			Date mBefore = calendar.getTime();
			time = sdf.format(mBefore);
		} catch (Exception e) {
			time = "";
		}
		return time;
	}

	/**
	 * 以友好的方式显示时间
	 *
	 * @param sdate
	 * @return
	 */
	public static String friendly_time(String sdate) {
		Date time = toDate(sdate);
		if (time == null) {
			return "Unknown";
		}
		String ftime = "";
		Calendar cal = Calendar.getInstance();

		// 判断是否是同一天
		String curDate = dateFormater2.format(cal.getTime());
		String paramDate = dateFormater2.format(time);
		if (curDate.equals(paramDate)) {
			int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
			if (hour == 0)
				ftime = Math.max(
						(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
						+ "分钟前";
			else
				ftime = hour + "小时前";
			return ftime;
		}

		long lt = time.getTime() / 86400000;
		long ct = cal.getTimeInMillis() / 86400000;
		int days = (int) (ct - lt);
		if (days == 0) {
			int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
			if (hour == 0)
				ftime = Math.max(
						(cal.getTimeInMillis() - time.getTime()) / 60000, 1)
						+ "分钟前";
			else
				ftime = hour + "小时前";
		} else if (days == 1) {
			ftime = "昨天";
		} else if (days == 2) {
			ftime = "前天";
		} else if (days > 2 && days <= 10) {
			ftime = days + "天前";
		} else if (days > 10) {
			ftime = dateFormater2.format(time);
		}
		return ftime;
	}

	/**
	 * 以类似qq发表说说的友好的方式显示时间
	 *
	 * @param sdate
	 * @return
	 */
	public static String friendly_time_qq(String sdate) {
		Date time = toDate(sdate);
		if (time == null) {
			return "Unknown";
		}
		String ftime = "";
		Calendar cal = Calendar.getInstance();

		// 判断是否同一年
		String curYear = year.format(cal.getTime());
		String paramYear = year.format(time);
		if (!curYear.equals(paramYear)) {
			ftime = dateFormater2.format(time);
			return ftime;
		}

		// 判断是否是同一天
		String curDate = day.format(cal.getTime());
		String paramDate = day.format(time);
		// 判断是否是同一月
		String curMonth = month.format(cal.getTime());
		String paramMonth = month.format(time);

		int lt = Integer.parseInt(paramDate);
		int ct = Integer.parseInt(curDate);
		int days = ct - lt;
		if (days == 0 && curMonth.equals(paramMonth)) {
			ftime = "今天 " + dateFormater3.format(time);
		} else if (days == 1 && curMonth.equals(paramMonth)) {
			ftime = "昨天 " + dateFormater3.format(time);
		} else if (days == 2 && curMonth.equals(paramMonth)) {
			ftime = "前天" + dateFormater3.format(time);
		} else if (days > 2 && curMonth.equals(paramMonth)) {
			ftime = dateFormater4.format(time);
		} else {// <0
			ftime = dateFormater4.format(time);
		}
		return ftime;
	}

	/**
	 * 以类似qq发表说说的友好的方式显示时间,可显示明天后天
	 *
	 * @param sdate
	 * @return
	 */
	public static String friendly_time_qq2(String sdate) {
		Date time = toDate(sdate);
		if (time == null) {
			return "Unknown";
		}
		String ftime = "";
		Calendar cal = Calendar.getInstance();

		// 判断是否同一年
		String curYear = year.format(cal.getTime());
		String paramYear = year.format(time);
		if (!curYear.equals(paramYear)) {
			ftime = dateFormater2.format(time);
			return ftime;
		}

		// 判断是否是同一天
		String curDate = day.format(cal.getTime());
		String paramDate = day.format(time);

		int lt = Integer.parseInt(paramDate);
		int ct = Integer.parseInt(curDate);
		int days = ct - lt;
		if (days == 0) {
			ftime = "今天 " + dateFormater3.format(time);
		} else if (days == 1) {
			ftime = "昨天 " + dateFormater3.format(time);
		} else if (days == 2) {
			ftime = "前天" + dateFormater3.format(time);
		} else if (days > 2) {
			ftime = dateFormater4.format(time);
		} else if (days == -1) {
			ftime = "明天 " + dateFormater3.format(time);
		} else if (days == -2) {
			ftime = "后天" + dateFormater3.format(time);
		} else if (days < -2) {
			ftime = dateFormater4.format(time);
		}
		return ftime;
	}

	/**
	 * 判断给定字符串时间是否为今日
	 *
	 * @param sdate
	 * @return boolean
	 */
	public static boolean isToday(String sdate) {
		boolean b = false;
		Date time = toDate(sdate);
		Date today = new Date();
		if (time != null) {
			String nowDate = dateFormater2.format(today);
			String timeDate = dateFormater2.format(time);
			if (nowDate.equals(timeDate)) {
				b = true;
			}
		}
		return b;
	}

	/**
	 * 打印长字符串
	 *
	 * @param veryLongString
	 */
	public static void printLongLog(String veryLongString) {
		int maxLogSize = 2000;
		for (int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
			int start = i * maxLogSize;
			int end = (i + 1) * maxLogSize;

			end = end > veryLongString.length() ? veryLongString.length() : end;

			System.out.println("长Json：\n"
					+ veryLongString.substring(start, end));
		}
	}

	/**
	 * 正则：手机号（精确）
	 * 移动：134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188
	 * 联通：130、131、132、145、155、156、175、176、185、186
	 * 电信：133、153、173、177、180、181、189
	 * 全球星：1349
	 * 虚拟运营商：170
	 *
	 * @return 匹配成功 true ,false 失败
	 */
	public static boolean isMobile(String mobileNum) {
		String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
		return mobileNum.matches(REGEX_MOBILE_EXACT);
	}

	/**
	 * 验证是否是正确手机号
	 *
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		/*
		 * Pattern p = Pattern
		 * .compile("^((13[0-9])|(15[^4,//D])|(18[0,5-9]))//d{8}$"); Matcher m =
		 * p.matcher(mobiles); return m.matches();
		 */
		// /^1[3|5|7|8]{1}[0-9]{9}$/
		// return mobiles.matches("^(13|15|17|18)\\d{9}$");// 如果是正确手机号返回true
		return mobiles.matches("^1[3|5|7|8]{1}[0-9]{9}$");// 如果是正确手机号返回true
	}

	/**
	 * 验证是否是正确手机号
	 * 最简单的方法:验证开头是1和数字的位数
	 *
	 * @param mobiles 待验证手机号
	 * @return 返回验证的结果
	 */
	public static boolean isMobileNO11(String mobiles) {
		if (TextUtils.isEmpty(mobiles)) {
			return false;
		}
		if (!mobiles.startsWith("1")) {
			return false;
		}
		if (mobiles.length() != 11) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是不是一个合法的电子邮件地址
	 *
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null || email.trim().length() == 0)
			return false;
		return emailer.matcher(email).matches();
	}

	/**
	 * 判断是否是一个合法的url
	 *
	 * @param urlString
	 * @return
	 */
	public static boolean CheckUrl(String urlString) {
		if (TextUtils.isEmpty(urlString)) {
			return false;
		}
		try {
			URL url = new URL(urlString);
			System.out.println("url 正确");
			return true;
		} catch (MalformedURLException e) {
			System.out.println("url 不可用");
			return false;
		}
	}

	/**
	 * 以什么为分割字符串
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static String[] splitStr(String str, String split) {
		try {
			String strTemp = str;
			String[] st = strTemp.split(split);// 这个就是你要的数组
			return st;
		} catch (PatternSyntaxException e) {
			return null;
		}
	}

	/**
	 * 以什么为分割字符串,字符数组转list
	 *
	 * @param str
	 * @param split
	 * @return
	 */
	public static List<String> splitStr2List(String str, String split) {
		List<String> list;
		try {
			String strTemp = str;
			String[] st = strTemp.split(split);// 这个就是你要的数组
			list = java.util.Arrays.asList(st);
		} catch (Exception e) {
			list = new ArrayList<String>();
		}
		return list;
	}

	/**
	 * 设置double格式
	 *
	 * @param d
	 * @return
	 */
	public static String getFormatDouble(double d) {
		DecimalFormat df = new DecimalFormat("#####0");
		String num = df.format(d);
		return num;
	}

	/**
	 * 深复制List
	 *
	 * @param src
	 * @param <T>
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static <T> List<T> deepCopy(List<T> src) throws IOException,
			ClassNotFoundException {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteOut);
		out.writeObject(src);

		ByteArrayInputStream byteIn = new ByteArrayInputStream(
				byteOut.toByteArray());
		ObjectInputStream in = new ObjectInputStream(byteIn);
		@SuppressWarnings("unchecked")
		List<T> dest = (List<T>) in.readObject();
		return dest;
	}

	/**
	 * 2012-01-01 12:12:12.0转为
	 *
	 * @param time
	 * @return
	 */
	public static String submitTime(String time) {
		String result = "";

		if (!isEmpty(time)) {
			if (time.length() >= 19) {
				result = time.substring(0, 19);
			} else {
				result = time;
			}
		}
		return result;
	}

	/**
	 * 获取字符串中第N次出现的字符位置的方法
	 *
	 * @param split
	 * @param string
	 * @param index
	 * @return
	 */
	public static int getCharacterPosition(String split, String string,
										   int index) {
		// 这里是获取"#"符号的位置
		Matcher slashMatcher = Pattern.compile(split).matcher(string);
		int mIdx = 0;
		while (slashMatcher.find()) {
			mIdx++;
			// 当"#"符号第二次出现的位置
			if (mIdx == index) {
				break;
			}
		}
		return slashMatcher.start();
	}

	/**
	 * 分转元 int
	 *
	 * @param price
	 * @return
	 */
	public static String getPrice(int price) {
		String str = String.format(Locale.getDefault(), "%.2f", price / 100.00);
		return str;
	}

	/**
	 * 分转元 String
	 *
	 * @param price
	 * @return
	 */
	public static String getPrice(String price) {
		String str = String.format(Locale.getDefault(), "%.2f", Integer.parseInt(price) / 100.00);
		return str;
	}

	/**
	 * 分转元 to double
	 *
	 * @param price
	 * @return
	 */
	public static double getPriceDouble(int price) {
		double str = price / 100.00;
		return str;
	}

	/**
	 * null转空
	 *
	 * @param input
	 * @return
	 */
	public static String null2Empty(String input) {
		if (input == null || "".equals(input) || "None".equals(input) || "null".equals(input))
			return "";

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return input;
			}
		}
		return "";
	}

	/**
	 * 获取银行卡账号, 保留后四位
	 *
	 * @param bankCard
	 */
	public static String getBankCardNum(String bankCard) {
		if (TextUtils.isEmpty(bankCard)) {
			return "";
		} else {
			return bankCard.replaceAll("(?<=\\d{0})\\d(?=\\d{4})", "*");
		}
	}

	/**
	 * 保密姓名只保留姓氏
	 */
	public static String getSecrecyName(String name) {
		String secrecyname = "";
		if (TextUtils.isEmpty(name)) {
			return "";
		} else if (name.length() == 2) {
			secrecyname = name.substring(0, 1) + "*";
			return secrecyname;
		} else if (name.length() == 3) {
			secrecyname = name.substring(0, 1) + "**";
			return secrecyname;
		} else if (name.length() > 3) {
			secrecyname = name.substring(0, 1);
			for (int i = 0; i < name.length() - 1; i++) {
				secrecyname += "*";
			}
			return secrecyname;
		} else {
			return name;
		}
	}

	/**
	 * 转换日期格式
	 *
	 * @param date        日期
	 * @param formpattern 原格式
	 * @param topattern   欲转换的格式
	 * @return 返回转换后的格式
	 */
	public static String convertDate(String date, String formpattern, String topattern) {

		String result = "";
		if (!TextUtils.isEmpty(date)) {
			try {
				Date time = null;
				SimpleDateFormat sdf = new SimpleDateFormat(formpattern);
				time = sdf.parse(date);
				SimpleDateFormat sdf2 = new SimpleDateFormat(topattern);
				result = sdf2.format(time);
			} catch (Exception e) {
				result = date;
			}
		}
		return result;
	}

	/**
	 * 转换成毫秒数
	 *
	 * @param date    日期
	 * @param pattern 日期格式
	 * @return 返回转换后的毫秒数
	 */
	public static long geTimeMillis(String date, String pattern) {
		long result = 0;
		SimpleDateFormat simpledate = new SimpleDateFormat(pattern);
		try {
			Date parsedate = simpledate.parse(date);
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(parsedate);
			result = cal.getTimeInMillis();

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取当前毫秒数
	 */
	public static long geTimeMillis() {
		Date date = new Date();
		long time = date.getTime();
		return time;
	}

	/**
	 * 获取当前的时间24小时计数法
	 */
	public static String getCurrentDate() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String format = sDateFormat.format(date);
		String currentdate = format;
		return currentdate;
	}

	/**
	 * 获取当前时间
	 */
	public static String getCurrentDate(String Pattern) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(Pattern);
		Date date = new Date();
		String format = sDateFormat.format(date);
		String currentdate = format;
		return currentdate;
	}

	/**
	 * 计算距离今天多少小时
	 */
	public static String getHourToToday(String date) {

		long millis = geTimeMillis(date, PATTERN1);
		long min_long = millis - System.currentTimeMillis();
		if (min_long > 0) {
			long hours_long = min_long / 1000 / 60 / 60;
			return hours_long + "小时";
		}
		return 0 + "小时";
	}

	/**
	 * 根据出生日期计算年龄
	 */
	public static String getAgeByBirthday(String date, String Pattern) {
		long millis = geTimeMillis(date, Pattern);
		long min_long = System.currentTimeMillis() - millis;
		if (min_long > 0) {
			long year = min_long / 1000 / 60 / 60 / 24 / 365;
			return String.valueOf(year);
		}
		return "未知年龄";
	}

	/**
	 * 判断当前日期是星期几
	 *
	 * @param date 设置的需要判断的时间  //格式如2012-09-08
	 * @return dayForWeek 判断结果
	 * @Exception 发生异常
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getWeek(String date, String pattern) {

		String Week = "";

		if (!TextUtils.isEmpty(date)) {
			SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
			Date date2 = null;
			try {
				date2 = dateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date2);

			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				Week += "星期日";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
				Week += "星期一";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
				Week += "星期二";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
				Week += "星期三";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
				Week += "星期四";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
				Week += "星期五";
			}
			if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
				Week += "星期六";
			}
		}
		return Week;
	}

	/**
	 * 通过url获取文件后缀名
	 *
	 * @param url
	 */
	public static void getFilenameByUrl(CharSequence url) {

		String suffixes = "avi|mpeg|3gp|mp3|mp4|wav|jpeg|gif|jpg|png|apk|exe|txt|html|zip|java|doc|pdf";
		Pattern pat = Pattern.compile("[\\w]+[\\.](" + suffixes + ")");//正则判断
		Matcher mc = pat.matcher(url);//条件匹配
		while (mc.find()) {
			String substring = mc.group();//截取文件名后缀名
			Log.e("substring:", substring);
		}
	}

	/**
	 * 防止字符串为null
	 */
	public static String getString(String string) {
		if (TextUtils.isEmpty(string)) {
			return "";
		} else if (string.equals("null")) {
			return "";
		}
		return string;
	}

	/**
	 * 字符串转整数数字
	 *
	 * @param str
	 * @param defaultInt
	 */
	public static int stringToInt(String str, int defaultInt) {
		try {
			int num = Integer.parseInt(str);
			return num;
		} catch (NumberFormatException e) {
			Log.e("stringToNum", "字符串转数字异常");
		}
		return defaultInt;
	}

	/**
	 * 字符 float型
	 *
	 * @param str
	 * @param defaultInt
	 */
	public static float stringToFloat(String str, float defaultInt) {
		try {
			float num = Float.parseFloat(str);
			return num;
		} catch (NumberFormatException e) {
			Log.e("stringToNum", "字符串转数字异常");
		}
		return defaultInt;
	}

	/**
	 * java精度加法法
	 *
	 * @param num
	 * @param tonum
	 */
	public static float BdAdd(String num, String tonum) {
		float ss = 0.0f;
		try {
			BigDecimal b = new BigDecimal(num);
			BigDecimal b2 = new BigDecimal(tonum);
			ss = b.add(b2).floatValue();
		} catch (NumberFormatException e) {
			return Float.valueOf(num);
		}
		return ss;
	}

	/**
	 * java精度减法
	 *
	 * @param num
	 * @param tonum
	 */
	public static float BdSubtract(String num, String tonum) {
		float ss = 0.0f;
		try {
			BigDecimal b = new BigDecimal(num);
			BigDecimal b2 = new BigDecimal(tonum);
			ss = b.subtract(b2).floatValue();
		} catch (NumberFormatException e) {
			return Float.valueOf(num);
		}
		return ss;
	}

	/**
	 * 乘法运算
	 *
	 * @param num
	 * @param tonum
	 * @return
	 */
	public static float BdMul(String num, String tonum) {
		float ss = 0.0f;
		try {
			BigDecimal b = new BigDecimal(num);
			BigDecimal b2 = new BigDecimal(tonum);
			ss = b.multiply(b2).floatValue();
		} catch (NumberFormatException e) {
			return Float.valueOf(num);
		}
		return ss;
	}

	/**
	 * 效验身份号
	 * @param idCard 身份证号
	 * @return  身份证效验结果
	 */
	public static boolean idCardValidate(String idCard) {
		//正则：身份证号码15位
		String REGEX_IDCARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
		//正则：身份证号码18位
		String REGEX_IDCARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
		//身份证号码15或18位 包含以x结尾
		String REGEX_IDCARD = "(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)$)";

		return idCard.matches(REGEX_IDCARD15) || idCard.matches(REGEX_IDCARD18) || idCard.matches(REGEX_IDCARD);
	}

}



